<?php

class IndustryAction extends HomeCommonAction {

    public function index() {
    	$this->categoryExplain();
        $id = intval($_GET['id']);
        if (empty($id)) {
            $id = 1;
        }
        $condition = array();
        $condition['industry_id'] = $id;

        //行业下品类
        $category = D('BaseCategory');
        $condition['pid'] = 0;
        $category_info = $category->where($condition)->order("py asc")->select(); //顶级
        $category_pid = intval($_GET['category_pid']);   //1级
        if ($category_pid > 0 ) {
            $condition['pid'] = $category_pid;
            $category_info1 = $category->where($condition)->order("py asc")->select();
            $category_id = $category_pid;
        }

        $category_pid1 = intval($_GET['category_pid1']); //2级
        if ($category_pid1 > 0) {
            $condition['pid'] = $category_pid1;
            $category_info2 = $category->where($condition)->order("py asc")->select();
            $category_id1 = $category_pid1;
        }

        $category_pid2 = intval($_GET['category_pid2']); //点击2级后的分类id
        if ($category_pid2 > 0) {
            $category_id2 = $category_pid2;
        }
        
        $brand_id = intval($_GET['brand_id']);
        if($brand_id > 0){
            $condition1 = " and brand_id = '$brand_id'";
        }
        
        //--codetaste--获取commodity列表
        $map['industry_id'] = $_REQUEST['id'];
        $map_brand['a.industry_id'] = $_REQUEST['id'];
        
        if($_GET['brand_id']) $map['brand_id'] = $_REQUEST['brand_id']; //品牌选择
        
		if($_GET['category_pid2']){
			$c_id = $_GET['category_pid2'];
		}elseif($_GET['category_pid1']){
			$c_id = $_GET['category_pid1'];
		}elseif($_GET['category_pid']){
			$c_id = $_GET['category_pid'];
		} //品类选择
		
		$c_arr = $this->getCategorys($c_id);
        $c_str = implode(",",$c_arr);
		if($c_str){
			$map['_string'] = "category_id in ($c_str)";
			$map_brand['_string'] = "a.category_id in ($c_str)";
		}
        $order = "score desc,add_time desc";
        $commodity_list = $this->_list(M("BaseCommodity"),$map,$order);
//        echo M("BaseCommodity")->getlastsql();
        //--获取是否被关注
        foreach($commodity_list as $k=>$v){
        	$commodity_list[$k]["is_follow"] = $this->getIsFollow($v['id']);
        }
        
        $this->getMyLishi(); //--获取我的历史浏览记录
        
		//--品牌列表
		unset($map['brand_id']);
//		dump($map_brand);
//        $brand_list = $this->getList(M("BaseCommodity"),$map,'py asc','','brand_id');
        $brand_list = M("BaseCommodity")->table('cm_base_commodity a')->join('cm_base_brand b on a.brand_id=b.id')->where($map_brand)->field('a.brand_id,b.name,b.py,b.id')->order('py asc')->group("a.brand_id")->findall();
//        dump(M("BaseCommodity")->getlastsql());
//        foreach($brand_list as $k=>$v){
//        	$brands[] = $this->getInfo(M("BaseBrand"),$v['brand_id']);
//        }
        
        
        
        $Ad = D("Ad");
        $ad_info = $Ad->where("type = '{$id}'")->order('id desc')->limit('0,6')->select();   
//        print_r($ad_info);
        $ad_info_right = $Ad->where("type = '{$id}_right'")->order('id desc')->limit('0,6')->select();
       
        $this->assign('ad_info', $ad_info);
        $this->assign('ad_info_right', $ad_info_right);
        $this->assign('category_pid', $category_pid);
        $this->assign('category_pid1', $category_pid1);
        $this->assign('category_pid2', $category_pid2);
        $this->assign('category_info', $category_info);
        $this->assign('category_info1', $category_info1);
        $this->assign('category_info2', $category_info2);
        $this->assign('brands', $brand_list);
        $this->assign('commodity_list', $commodity_list);
        $this->assign('id', $id);
        $this->assign('brand_id', $brand_id);
        $this->assign('hangye', $hangye);        
        $this->assign('memo', $memo);  
        
        
        //--codetaste--我关注的商品和我浏览的商品
        $follows = $this->getMyFollow();
        
        
        
        
        $this->display('index');
    }

    public function searchBrand() {

        $word = $_GET['brand_word'];
        $condition = array();
        $BaseBrand = D('BaseBrand');
        if (!empty($word)) {
            $condition['py'] = array('like', "%{$word}%");
        }

        $brand = $BaseBrand->where($condition)->order('id desc')->limit()->select();
        $this->assign('brand', $brand);
        $this->display('searchBrand');
    }

    public function searchCategory() {
        $word = $_GET['brand_word'];
        $condition = array();
        $condition['name'] = $word;
        $BaseBrand = D('BaseBrand');
        $brand = $BaseBrand->where($condition)->find();

        $BaseCategory = D('BaseCategory');
        $condition = array();
        $condition['brand_id'] = $brand['id'];

        $category = $BaseCategory->where($condition)->order('id desc')->limit()->select();
        $this->assign('category', $category);
        $this->display('searchCategory');
    }

    public function searchCommodity() {
        $word = $_GET['brand_wod'];
        $condition = array();
        $condition['name'] = $word;
        $BaseBrand = D('BaseBrand');
        $brand = $BaseBrand->where($condition)->find();

        $word = $_GET['category_word'];
        $condition = array();
        $condition['name'] = $word;
        $condition['brand_id'] = $brand['id'];
        $BaseCategory = D('BaseCategory');
        $category = $BaseCategory->where($condition)->find();


        $condition = array();
        $BaseCommodity = D('BaseCommodity');
        $condition['brand_id'] = $brand['id'];
        $condition['category_id'] = $category['id'];


        $commodity = $BaseCommodity->where($condition)->order('id desc')->limit()->select();
        $this->assign('commodity', $commodity);
        $this->display('searchCommodity');
    }
	/**
	 * codetaste
	 */
    public function getIsFollow($commodity_id){
    	$map['uid'] = Session::getLocal("id");
    	$map['commodity_id'] = $commodity_id;
    	$map['status'] = 1;
    	$info = $this->getInfo(M("BaseFollow"),'', $map);
    	return $info['id'] ? true : false;
    }
    /**
     * codetaste
     * 获得我的关注
     */
    public function getMyFollow(){
    	$map_c['uid'] = Session::getLocal('id');
    	$map_c['status'] = 1;
    	$map_c['is_show'] = 1;
    	$sp_list = $this->getList(M("BaseFollow"),$map_c,'','0,5');
    	foreach($sp_list as $k=>$v){
    		$sp_list[$k]["info"] = $this->getInfo(M("BaseCommodity"),$v['commodity_id']);
    		$sp_list[$k]["brand_info"] = $this->getInfo(M("BaseBrand"),$sp_list[$k]["info"]['brand_id']);
    		$sp_list[$k]['new_tiezi_num'] = $this->getNewTieziNum($v['commodity_id'],$v['tiezi_num']);
    		$sp_list[$k]['sp_count'] = $this->getCount(M("BaseFollow"), array("commodity_id"=>$v['commodity_id'])); //商品关注数
    	}
    	$this->assign("sp_list",$sp_list);
    }
    
    /**
     * codetaste
     * 获得新帖子数
     */
    public function getNewTieziNum($id,$tiezi_num){
    	$count = M("Share")->where("commodity_id='$id'")->count();
    	$num = number_format($count-$tiezi_num);
    	return $num;
    }
    
    /**
     * codetaste
     * 获得我的历史浏览记录
     */
    public function getMyLishi(){
    	$lishi_commodity = $_COOKIE["lishi_commodity"];
    	$lishi_commodity = str_replace("\\","",$lishi_commodity);
    	$lishi_commoditys = unserialize(unserialize($lishi_commodity));
    	foreach($lishi_commoditys as $k=>$v){
    		$lishi[$k] = $this->getInfo(M("BaseCommodity"),$v);
    		$lishi[$k]['brand_info'] = $this->getInfo(M("BaseBrand"), $lishi[$k]['brand_id']);
    	}
    	$this->assign("lishi", $lishi);
    }
    
    
    public function getCategorys($id){
    	$categorys_1 = $this->getZiCategorys($id);
		if($categorys_1){
    		$category_all_id = $categorys_1;  //将第二级id赋值给 $category_all_id;
	        foreach($categorys_1 as $k=>$v){
		    	$category_ids = $this->getZiCategorys($v);
		    	if($category_ids){
		    		$category_all_id = array_merge($category_all_id,$category_ids);
		    	}
	        }
		}
        $category_all_id[] = $id;
        return $category_all_id;
    }
    
    
    function getZiCategorys($id){
    	$map['pid'] = $id;
    	$categorys = $this->getListField(M("BaseCategory"),$map,"id");
    	
    	foreach($categorys as $k=>$v){
        	$category_ids[] = $v['id'];
        }
    	return $category_ids;
    }
    
    function categoryExplain(){
    	$category['1'] = "美容化妆 个护健康 户外装备 首饰服饰 玩具等个人生活用品";
    	$category['2'] = "办公耗材 办公家具 办公仪器设备等办公用品";
    	$category['3'] = "家居装饰 生活家具 厨房用具 洗浴用具等家庭生活用品";
    	$category['4'] = "手机数码 摄影摄像 大小家电";
    	$category['5'] = "家用汽车 汽车装饰品 汽车用品";
    	$this->assign("industry_explain", $category[$_GET['id']]);
    }
}

?>
